const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const router = require('./router')
const errorHandler = require('./middleware/error-handler')
require('./model')

const app = express()
const PORT = process.env.PORT || 3000

// 配置日志
app.use(morgan('dev'))

// 解析请求体中间件
app.use(express.json())
app.use(express.urlencoded())

// 允许跨域请求
app.use(cors())

// 挂载路由器
app.use('/api', router)

// 挂载统一处理服务端错误的中间件
router.get(errorHandler())
app.listen(PORT, () => console.log(`Example app listening on PORT ${PORT}!`))
